package com.evaluationsystem.system.controller;

import cn.hutool.core.date.DateUtil;
import com.evaluationsystem.system.serivce.AssessStatisticalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("AssessStatistical")
@Api(tags = "考核统计",value = "2020-4-21-gdy")
public class AssessStatisticalController {

    @Autowired
    AssessStatisticalService service;

    @GetMapping("")
    @ApiOperation(value = "考核统计 页面跳转 ",notes = "2020-4-21-gdy")
    public String AssessStatisticalhtml(){
        return "system/assessStatistical/assessStatistical";
    }

    @ResponseBody
    @ApiOperation(value = "考核统计 Table 数据 接口",notes = "2020-4-22-gdy")
    @ApiImplicitParam(name = "param",value = "{page=1, limit=10,number=1}",required = true,paramType = "json")
    @PostMapping("StatisticalTable")
    public Map<String,Object> StatisticalTable(@RequestBody Map<String,Object> param){
        Map<String,Object> res = service.StatisticalTable(param);
        return res;
    }

    @ApiOperation(value = "考核统计 Excel 导出 接口",notes = "2020-4-23-gdy")
    @ApiImplicitParam(name = "param",value = "{vpositionid=职位,vfactoryflg=工厂,vteamid=班组,vappraiseflg=评定组,start=开始评定日期,end=结束评定日期}",required = false,paramType = "string")
    @GetMapping("exportExcel")
    public void exportExcel(@RequestParam Map<String,Object> param, HttpServletResponse response){
        try {
            Workbook wb = service.exportExcel(param);
            //编辑 Excel 的 文件名
            String fileName = "考核统计报表 - "+ DateUtil.today() +".xlsx";
            response.setContentType("application/octet-stream");
            response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"),"iso-8859-1"));
            response.flushBuffer();
            wb.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
